package com.aos.controller;

import com.aos.dto.parameters.RoleParameter;
import com.aos.permission.RequiredPermission;
import com.aos.service.application.ApplicationService;
import com.aos.service.role.RoleService;
import com.aos.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName RoleController
 * @Description:角色控制层
 * @Author wumingwang
 * @Date 2018/9/9 17:38
 * @Version 1.0
 */
@Slf4j
@RestController
@Api(value = "Role Controller", tags = {"角色接口"})
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private ApplicationService applicationService;

    /**
     * 分页查询角色
     * @param roleName
     * @param applicationId
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "查询角色", notes = "分页查询角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleName", value = "角色名称",  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "applicationId", value = "应用ID",  paramType = "query", dataType = "String",example = "0"),
            @ApiImplicitParam(name = "page", value = "页码", required = true, paramType = "query", dataType = "int" ,example = "1"),
            @ApiImplicitParam(name = "limit", value = "页面大小", required = true, paramType = "query", dataType = "int",example = "10"),
    })
    @RequiredPermission
    @GetMapping("/role/v1/queryByPage")
    public Object queryByPage(String roleName,Long applicationId,int page, int limit){
        Map<String,Object> map = new HashMap<>();
        map.put("roleName",roleName);
        map.put("applicationId",applicationId);
        map.put("pageNum",page);
        map.put("pageSize",limit);
        return roleService.queryByPage(map);
    }

    /**
     * 新增角色
     * @param roleParameter
     * @return
     */
    @ApiOperation(value = "新增角色", notes = "新增角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleName", value = "角色名称",  required = true, dataType = "String"),
            @ApiImplicitParam(name = "applicationId", value = "应用ID", required = true, dataType = "Long",example = "0"),
            @ApiImplicitParam(name = "comment", value = "备注", dataType = "String"),
            @ApiImplicitParam(name = "permissions", value = "菜单ID数组", required = true, dataType = "Long[]"),
    })
    @RequiredPermission
    @PostMapping("/role/v1/add")
    public Object addRole(@Validated @RequestBody RoleParameter roleParameter){
        if(roleParameter.getApplicationId() == null){
            return Result.error("应用ID不能为空!");
        }
        return roleService.addRole(roleParameter);
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @ApiOperation(value = "删除角色", notes = "删除角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色ID",  required = true, dataType = "Long",example = "0"),
    })
    @RequiredPermission
    @DeleteMapping("/role/v1/delete")
    public Object deleteRole(Long id){
        if(id == null){
            return Result.error("角色ID不能为空！");
        }
        return roleService.deleteById(id);
    }

    /**
     *编辑角色
     * @param roleParameter
     * @return
     */
    @ApiOperation(value = "编辑角色", notes = "编辑角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色ID",  required = true, dataType = "Long", example = "0"),
            @ApiImplicitParam(name = "roleName", value = "角色名称",  required = true, dataType = "String"),
            @ApiImplicitParam(name = "comment", value = "备注", dataType = "String"),
            @ApiImplicitParam(name = "permissions", value = "菜单ID数组", required = true, dataType = "Long[]"),
    })
    @RequiredPermission
    @PostMapping("/role/v1/update")
    public Object updateRole(@Validated @RequestBody  RoleParameter roleParameter){
        if(roleParameter.getId() == null){
            return Result.error("角色ID不能为空！");
        }
        return roleService.updateRole(roleParameter);
    }

    /**
     * 查询角色详情
     * @param id
     * @return
     */
    @ApiOperation(value = "查询角色详情", notes = "查询角色详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色ID",  required = true, dataType = "Long",example = "0"),
    })
    @RequiredPermission
    @GetMapping("/role/v1/queryDetails")
    public Object queryDetails(Long id){
        if(id == null){
            return Result.error("角色ID不能为空！");
        }
        return roleService.queryDetails(id);
    }

    /**
     * 查询应用下的角色列表
     * @param appId
     * @return
     */
    @ApiOperation(value = "查询角色列表", notes = "查询某应用下的角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "应用ID",  required = true, dataType = "Long",example = "0"),
            @ApiImplicitParam(name = "userId", value = "用户ID",  dataType = "Long",example = "0"),
    })
    @GetMapping("/role/v1/queryList")
    public Object queryList(Long appId,Long userId){
        if(appId == null){
            return Result.error("应用ID不能为空！");
        }
        return roleService.queryListByAppId(appId,userId);
    }

    /**
     * 新增角色
     * @param roleParameter
     * @return
     */
    @ApiOperation(value = "新增角色", notes = "根据应用code新增角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleName", value = "角色名称",  required = true, dataType = "String"),
            @ApiImplicitParam(name = "appCode", value = "应用code", required = true, dataType = "String"),
            @ApiImplicitParam(name = "comment", value = "备注", dataType = "String"),
            @ApiImplicitParam(name = "permissions", value = "菜单ID数组", required = true, dataType = "Long[]"),
    })
    @PostMapping("/role/v1/addByAppCode")
    public Object addRoleByAppCode(@Validated @RequestBody RoleParameter roleParameter){
        if(StringUtils.isEmpty(roleParameter.getAppCode())){
            return Result.error("应用code不能为空！");
        }
        return roleService.addRoleByAppCode(roleParameter);
    }

    /**
     * 查询应用下的角色列表
     * @param appCode
     * @param userId
     * @return
     */
    @ApiOperation(value = "查询角色列表", notes = "查询某应用下的角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "应用ID",  required = true, dataType = "Long",example = "0"),
            @ApiImplicitParam(name = "userId", value = "用户ID",  dataType = "Long",example = "0"),
    })
    @GetMapping("/role/v1/queryListByAppCode")
    public Object queryListByAppCode(String appCode,Long userId){
        if(StringUtils.isEmpty(appCode)){
            return Result.error("应用code不能为空！");
        }
        return roleService.queryListByAppCode(appCode,userId);
    }

}
